<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2019-1-3
 * Time: 10:41
 */

namespace app\index\controller;


use app\common\model\MemberModel;
use app\common\model\OrderModel;
use app\common\model\GoodsModel;
use think\Cookie;
use think\Request;
use think\Session;

class Order extends Base
{
//    public function _initialize()
//    {
//        //防恶意调用接口
//        if(!isset($_SERVER['HTTP_REFERER']) || !stripos($_SERVER['HTTP_REFERER'],'www.jb51.net')) {
//            echo '没有权限访问';
//            exit();
//        }
//    }
    protected function getUname(){
        $phone = Session::get('phone');
        $name = Session::get('name');
        $uname = empty($name)?$phone:$name;
        if (!empty($uname)){
            $this->assign([
                'uname'=>$uname
            ]);
        }
    }

    public function postorder()
    {
        $this->getUname();
        $member = new MemberModel();
        $id = Session::get('id');
        $user = $member->getOneMember($id,"memb_city,memb_address,memb_addrName,memb_addrPhone");

        $code = md5(rand(0,99999999));
        Cookie::set('ocode',$code,3600);
        $this->assign([
            'user'=>$user,
            'code'=>$code
        ]);

        return $this->fetch();
    }

    //订单页面
    public function order()
    {
        $this->getUname();
        $order = new OrderModel();
        $where['orde_uid'] = Session::get('id');
        $list = $order->where($where)->order('orde_id desc')->paginate(10);

        $this->assign([
            'list'=>$list
        ]);

        return $this->fetch();
    }

    public function createOrder(){//下单
        if (request()->isPost()){

            $req = Request::instance()->param();

            if (Cookie::get('ocode')){
                if ($req['code'] != Cookie::get('ocode')){
                    return json(['code'=>404,'msg'=>'下单失败']);
                }
            }else{
                return json(['code'=>404,'msg'=>'下单超时']);
            }


            $order = new OrderModel();
            $goods = new GoodsModel();
            $user = new MemberModel();

            $good_id = Cookie::get('good_id');

            $data = $goods->getOneGoods($good_id);

            //更新库存
            $goods->where('good_id',$data['good_id'])->setDec('good_repertory', $req['orde_num']);

            $req['orde_price'] = $data['good_price'];//单价
            $req['orde_name'] = $data['good_name'];//订单名称
            $req['orde_image'] = $data['good_img'];//订单名称
            $user_id = Session::get('id');

            $userinfo = $user->getOneMember($user_id);

            $req['orde_uid'] = Session::get('id');
            $req['orde_userPhone'] = $userinfo['memb_addrPhone'];//收件人手机号
            $req['orde_userName'] = $userinfo['memb_addrName'];//收件人
            $req['orde_city'] = $userinfo['memb_city'];//市
            $req['orde_address'] = $userinfo['memb_address'];//详细地址


            unset($req['id']);
            unset($req['code']);
            $req['orde_createTime'] = date('Y-m-d H:i:s');
            $flag = $order->addOrder($req);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));

        }
        return json(['code'=>201,'msg'=>'下单成功']);
    }
}